
//
//  ZYHomeCouseListCell.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/25.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYHomeCouseListCell.h"
#import "ZYHomeCouseListsssModel.h"
#import "UIButton+ZYWebCaches.h"
@implementation ZYHomeCouseListCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setUpSuperView];
    }
    
    
    
    return self;
    
}



- (void)assignByZYHomeCouseListsssModel:(ZYHomeCouseListsssModel *)model{
    
    self.photoImageView.hidden = YES;
    self.photoTwoImageView.hidden = YES;
    self.playButton.hidden = YES;
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@",model.title];
    self.timeLabel.text = [NSString stringWithFormat:@"%@",model.createTime];
    self.couseLabel.text = [NSString stringWithFormat:@"课程名字：%@",model.lessonName];
    
    
    BOOL isUp = [NSString isBlankString:model.content];
    
    if (isUp) {
        self.contendenLabel.text = @"";
    }else{
        self.contendenLabel.text = [NSString stringWithFormat:@"%@",model.content];
    }
    
    
    if (model.paths != nil && ![model.paths isKindOfClass:[NSNull class]] && model.paths.count != 0){
        
        
        
        if (model.paths.count > 0) {
            
            [model.paths enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
                if (idx == 0) {
                    
                    NSString *Str = [obj pathExtension];
                    if ([Str isEqualToString:@"amr"] || [Str isEqualToString:@"mp3"]) {
                        self.photoImageView.image = [UIImage imageNamed:@"btn_mp3"];
                    } else {
                        [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.paths[idx]] placeholderImage:[UIImage imageNamed:@"bg_img_homework_picture_default"]];

                    }
                    self.photoImageView.hidden = NO;
                } else  {
                
                    NSString *Str = [obj pathExtension];
                    Str = [Str pathExtension];
                    if ([Str isEqualToString:@"amr"] || [Str isEqualToString:@"mp3"]) {
                        self.photoTwoImageView.image = [UIImage imageNamed:@"btn_mp3"];
                    } else {
                        [self.photoTwoImageView sd_setImageWithURL:[NSURL URLWithString:model.paths[idx]] placeholderImage:[UIImage imageNamed:@"bg_img_homework_picture_default"]];
                        
                    }
                    self.photoTwoImageView.hidden = NO;
                }
                
            }];
    
            
            
            
        }
        
    }
    
    
    
    BOOL isUpTwo = [NSString isBlankString:model.video];
    
    if (isUpTwo) {
        
    }else{
        
        //       [self.playButton zy_setBackgroundImageWithUrl:model.video];
        
        [self.playButton setBackgroundImage:[self GetVideoPreViewImage:model.video] forState:self.playButton.state];
        self.playButton.hidden = NO;
        
    }
    
    
    
    
    if (0 == [model.count integerValue]) {
        
        self.circleProgressView.percent = 0.0;
        self.circleProgressView.centerLabel.text = [NSString stringWithFormat:@"%@/%@",model.count,model.totalCount];
        
    }else{
        
        
        float mm = [model.count floatValue];
        float ff = [model.totalCount floatValue];
        
        float hh = mm/ff;
        
        self.circleProgressView.percent = hh;
        self.circleProgressView.centerLabel.text = [NSString stringWithFormat:@"%@/%@",model.count,model.totalCount];
        
    }
    
    
    
    
    
}


/** 获取视频的第一帧的图片 */
- (UIImage*)GetVideoPreViewImage:(NSString *)Str
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:Str] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *img = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return img;
}



- (void)setUpSuperView{
    
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    self.nameLabel.textColor = ColorRGB(51, 51, 51);
    [self.contentView addSubview:self.nameLabel];
    
    
    self.couseLabel = [UILabel new];
    self.couseLabel.font = [UIFont systemFontOfSize:14];
    self.couseLabel.textColor = ColorRGB(102,102,102);
    self.couseLabel.numberOfLines = 0;
    [self.contentView addSubview:self.couseLabel];
    
    
    self.contendenLabel = [UILabel new];
    self.contendenLabel.font = [UIFont systemFontOfSize:14];
    self.contendenLabel.textColor = ColorRGB(102,102,102);
    self.contendenLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contendenLabel];
    
    
    self.photoImageView = [UIImageView new];
    [self.contentView addSubview:self.photoImageView];
    
    
    self.playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"bg_img_homework_picture_default"] forState:(UIControlStateNormal)];
    [self.contentView addSubview:self.playButton];
    
    
    self.photoTwoImageView = [UIImageView new];
    [self.contentView addSubview:self.photoTwoImageView];
    
    
    self.timeLabel = [UILabel new];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = ColorRGB(102,102,102);
    [self.contentView addSubview:self.timeLabel];
    
    
    
    
    
    
    self.photoImageView.hidden = YES;
    self.photoTwoImageView.hidden = YES;
    self.playButton.hidden = YES;
    
    
    self.circleProgressView = [[CircleProgressView alloc]initWithFrame:CGRectMake(kScreenWidth - (kScreenWidth *86/750 + kScreenWidth *30/750) - 10 , kScreenHeight *60/1334, kScreenWidth *86/750, kScreenWidth *86/750)];
    
    
    self.circleProgressView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.circleProgressView];
    
    self.nameLabel.text = @"北京上海";
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kScreenWidth *33/750);
        make.top.offset(kScreenHeight *30/1334);
        make.right.equalTo(self).offset(-kScreenWidth *86/750);
    }];
    
    
    
    
    [self.couseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self).offset(-kScreenWidth *86/750);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(kScreenHeight *30/1334);
    }];
    
    UILabel *lineLabelOne = [UILabel new];
    lineLabelOne.backgroundColor = ColorRGB(247, 245, 245);
    [self.contentView addSubview:lineLabelOne];
    
    [lineLabelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.couseLabel);
        make.top.equalTo(self.couseLabel.mas_bottom).offset(kScreenHeight *35/1334);
        make.width.offset(kScreenWidth -(kScreenWidth *33/750)*2);
        make.height.offset(1);
    }];
    
    
    [self.circleProgressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-kScreenWidth *30/750);
        make.centerY.equalTo(self.couseLabel);
        make.width.height.offset(kScreenWidth *86/750);
    }];
    
    
    
    self.contendenLabel.text = @"beinggsklgjlk";
    [self.contendenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineLabelOne);
        make.top.equalTo(lineLabelOne.mas_bottom).offset(kScreenHeight *26/1334);
    }];
    
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contendenLabel);
        make.top.equalTo(self.contendenLabel.mas_bottom).offset(kScreenHeight *26/1334);
        make.width.height.offset(kScreenWidth *100/750);
    }];
    
    
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.photoImageView.mas_right).offset(kScreenWidth *30/750);
        make.centerY.equalTo(self.photoImageView);
        make.width.height.offset(kScreenWidth *100/750);
    }];
    
    
    [self.photoTwoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playButton.mas_right).offset(kScreenWidth *30/750);
        make.centerY.equalTo(self.playButton);
        make.width.height.offset(kScreenWidth *100/750);
    }];
    
    
    
    
    
    
    
    
    self.timeLabel.text = @"布置时间：2012.3.11";
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self).offset(-kScreenWidth *30/750);
        make.top.equalTo(self.playButton.mas_bottom).offset(kScreenHeight *26/1334);
    }];
    
    
    
    
    
    
    
    
    UILabel *lineLabel = [UILabel new];
    lineLabel.backgroundColor = ColorRGB(247, 245, 245);
    [self.contentView addSubview:lineLabel];
    
    
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(kScreenHeight *48/1334);
        make.width.offset(kScreenWidth);
        make.height.offset(kScreenHeight *30/1334);
    }];
    
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.bottom.mas_equalTo(lineLabel).offset(0);
    }];
    
    
}












@end

